<template>
    <div class="zixun-content">
        <div class="zixun-content-edit" @click="goEdit">{{edit?"取消":"编辑"}}</div>
        <div class="zixun-content-item" v-for="(item,index) in selectedList" :key="index">
            <div class="zixun-content-item-right">
                <div v-if="edit" class="zixun-content-item-right-check">
                    <input type="checkbox" :id="item.visitId" class="chk_1" ref="checkbox" @change="singleSelect" :cId="item.visitId" />
                    <label :for="item.visitId"></label>               
                </div>
                <div @click="goArtical(item.informationId)" class="zixun-content-item-right-content" >
                    <p>{{item.title}}</p>
                    <div class="zixun-content-item-right-content-author clearfix">
                        <div class="zixun-content-item-right-content-log">
                             <img :src="item.head_img" alt="产品">
                         </div>
                         <p>{{item.nick_name}}</p>
                         <p>{{item.create_time}}</p>
                    </div>
                    <!-- <span>{{item.create_time}}</span> -->
                    <div class="zixun-content-item-right-content-icon clearfix">
                        <a href="javascript:void(0)"><img src="../../../../assets/comment/pinglun@2x.png" alt=""><span class="red" ref="a">{{item.discuss_count | number}}</span></a> 
                        <a href="javascript:void(0)"><img src="../../../../assets/comment/guanzhu@2x.png" alt=""><span>{{item.collectCount |  number}}</span></a>
                         <a href="javascript:void(0)"><img src="../../../../assets/comment/dianzan@2x.png" alt=""><span>{{item.favorCount |  number}}</span></a>
                        <a href="javascript:void(0)">
                            <img src="../../../../assets/comment/liulanliang@2x.png" alt="">
                            <span>{{item.readsCount |  number}}</span>
                        </a>   
                    </div>
                </div>     
            </div>
        </div>
        <load-more :tip="('加载中...')" v-if="loadMoreShow"></load-more>
        <!-- 如果一条帖子也没有，那么空的图片则显示 -->
        <img src="../../../../assets/nothing.png" alt="" v-if="this.selectedList.length==0">
        <load-more :show-loading="false" :tip="('没有更多了')" background-color="#f5f5f5" v-if="noMoreShow"></load-more>
        <div class="deleteDo" v-if="edit">
            <div class="deleteDo-left">
                <label for="checkAll">
                    <input type="checkbox" id="checkbox_a1" class="chk_1 checkall" ref="allSelect" @change="allSelect" />
                    <label for="checkbox_a1"></label>
                    <!-- <input type="checkbox" ref="allSelect" @change="allSelect" />全选 -->
                </label>
                <span style="position:absolute;">全选</span>
                <span>{{selectNum}}/{{selectedList.length}}</span>
            </div>
            <div class="deleteDo-right" @click="shanchu">删除</div>
        </div>
    </div>
</template>
<script>
import { dateFormat, LoadMore, cookie } from 'vux';
import { MessageBox, Toast } from 'mint-ui';
export default {
    components: { LoadMore },
    title: '帖子',
    data() {
        return {
            pageNum: 1,
            selectedList: [],
            flag: true,
            noMoreShow: false,
            loadMoreShow: false,
            value: 123,
            arr: [],
            idStr: '',
            edit: false,
            selectNum: 0,
            dedaultImg:{userimg:'static/img/user.png'} //表示的是用来处理默认图片的
        }
    },
    created() {
        var _this = this;
        this.$nextTick(function() {
            window.addEventListener('scroll', onScroll)
        })
        // 判断滚动条位置，加载更多
        function onScroll() {
            var scrollTop = document.body.scrollTop;
            var innerHeight = window.innerHeight;
            var offsetHeight = document.body.offsetHeight;
            if (scrollTop + innerHeight >= offsetHeight) {
                if (_this.flag) {
                    if (_this.selectedList.length >= 8) {
                        _this.flag = false;
                        _this.getList()
                    }
                }
            }
        }
    },
    mounted() {
        this.noMoreShow = false;
        var token = cookie.get('token');
        // "/loansupermarket-app/information/getCustomInformationVisit?pageNum=" + _this.pageNum + "&&pageSize=6"+'&visitType='+1+"&token="+token
        // 加载第一页数据并实例化
        this.$http.get("/loansupermarket-app/foot/getCustomInformationVisit?pageNum=" + this.pageNum + "&&pageSize=8" + '&visitType=' + 2 + "&token=" + token)
            .then((response) => {
            
                var resBody = response.body;
                if (resBody.code == '000') {
                    this.selectedList = resBody.data;
                    if (this.selectedList.length < 8) {
                        this.flag = false;
                        this.noMoreShow = true; 
                    }
                    // 时间戳转成2018-10-08格式
                    this.selectedList.forEach((item, index)=>{
                        item.create_time = dateFormat(item.create_time, 'YYYY-MM-DD')
                        if(item.head_img==null||item.head_img==""){ //注意这里用来判断是否后台传进来的为空，然后显示默认的图片
                           item.head_img=this.dedaultImg.userimg;
                        } 
                        //这里用来进行如果后台传入的值是空的就用电话号码代替
                        if(item.nick_name==null||item.head_img==""){
                             item.nick_name=item.phone;
                        }
                    })
                }else if(resBody.code == '444'){
                    this.$router.push({path:'/login',query:{history:1}});
                }else{
                    MessageBox.alert(resBody.mesg);
                }

            })
    },
    methods: {
        // 去编辑
        goEdit() {
            this.edit = !this.edit;
            if (!this.edit) {
                this.selectNum = 0;
            }
        },
        // 单选
        singleSelect() {
           
            var _this = this;
            var flag = true;
            this.$refs.checkbox.forEach(function(item, index) {
                if (!item.checked) {
                    _this.$refs.allSelect.checked = false;
                    flag = false;
                    return false;
                }
                if (flag) {
                    _this.$refs.allSelect.checked = true;
                }
            })
            // 判断已选列表个数
            this.judgeNum();
        },
        // 全选
        allSelect() {
            if (this.$refs.allSelect.checked) {
                this.$refs.checkbox.forEach(function(item, index) {
                    item.checked = true;
                })
            } else {
                this.$refs.checkbox.forEach(function(item, index) {
                    item.checked = false;
                })
            }
            this.judgeNum()
        },
        // 判断已选列表个数
        judgeNum() {
            var _this = this;
            _this.selectNum = 0;
            this.$refs.checkbox.forEach(function(item, index) {
                if (item.checked) {
                    _this.selectNum += 1
                }
            })
        },
        // 删除
        shanchu() {
            var _this = this;
            var token = cookie.get("token");
            this.$refs.checkbox.forEach(function(item, index) {
                if (item.checked) {
                    _this.arr.push(item.getAttribute('cId'));
                    _this.idStr = _this.arr.join(',');
                }
            })

            if (this.idStr.length == 0) {
                Toast('请勾选你要删除的帖子~');
            } else {
                this.$http.post('/loansupermarket-app/foot/deleteInformationVisit', { visitType: 2, idStr: this.idStr, token: token }).then(function(response) {
                    if (response.body.code == '000') {
                        Toast('删除成功！');
                        setTimeout(function() {
                            window.location.reload()
                        }, 500)
                    } else if (response.body.code == '444') {
                        this.$router.push({ path: '/login', query: { history: 1 } });
                    } else {
                        MessageBox.alert(response.body.mesg);
                    }
                })
            }
        },
        goArtical(id) {
            this.$router.push({ path: '/main/mydiscover/artical', query: { id: id } })
        },
        // 上拉加载数据
        getList() {
            this.pageNum += 1;
            this.loadMoreShow = true;
            var token = cookie.get('token');
            var _this = this;
            setTimeout(function() {
                _this.$http.get("/loansupermarket-app/foot/getCustomInformationVisit?pageNum=" + _this.pageNum + "&pageSize=8" + '&visitType=' + 2 + "&token=" + token)
                    .then((response) => {
                        var resBody = response.body;
                        if (resBody.code == '000') {
                            _this.flag = true;
                            _this.loadMoreShow = false;
                            if (_this.edit) {
                                if (resBody.data.length == 0) {
                                    _this.$refs.allSelect.checked = true;
                                } else {
                                    _this.$refs.allSelect.checked = false;
                                }
                            }
                            if (resBody.data.length < 8) {
                                _this.noMoreShow = true;
                                _this.flag = false;
                            } else {
                                _this.flag = true;
                            }
                            _this.selectedList = _this.selectedList.concat(resBody.data);
                           // 时间戳转成2018-10-08格式
                            _this.selectedList.forEach((item, index)=>{
                                item.create_time = dateFormat(item.create_time, 'YYYY-MM-DD')
                                if(item.head_img==null||item.head_img==""){ //注意这里用来判断是否后台传进来的为空，然后显示默认的图片
                                item.head_img=_this.dedaultImg.userimg;
                                } 
                                //这里用来进行如果后台传入的值是空的就用电话号码代替
                                if(item.nick_name==null||item.head_img==""){
                                    item.nick_name=item.phone;
                                }
                            })
                        } else if(resBody.code == '444') {
                            this.$router.push({path:'/login',query:{history:1}});
                        }
                    })
            }, 500)
        }
    }
}
</script>
<style lang="scss" scoped>
@import '../../../../scss/variable/base.scss';
.zixun-content {
    margin-top: 2.9rem;
    // width: 100%;
    box-sizing: border-box;
    &-edit {
        position: fixed;
        right: 0.4rem;
        top: 0.35rem;
        z-index: 999;
        color: #fff;
        font-size: 0.4rem;
    }
    .weui-loadmore_line {
        margin-top: 0.6rem;
    } 
    &-item {
        padding: 0.4rem 0.4rem rem(30) 0;
        background: #fff;
        box-sizing: border-box;
        width: 100%;
        margin-top: rem(20);
        &-right {
            margin-left: 0.27rem;
            text-align: left;
            display: flex;
            &-check {
                flex:1;
                input {
                    margin-top: 0.45rem;
                    width: 0.4rem;
                    height: 0.5rem;
                    border-radius: 50%;
                }
            }
            &-content {
                flex:9;    
                p {
                    font-size:rem(32); // height: 0.7rem;
                    line-height: 0.7rem;
                    color: #333333;
                    overflow: hidden;
                    
                }
                &-author{
                    
                     margin-top:rem(20);
                     p{
                        line-height:1rem;
                        float: left;
                     }
                     p:nth-of-type(1){
                         font-size:rem(26);
                         color: #666666;
                         margin:0 rem(20) 0 rem(10);
                        
                     }
                      p:nth-of-type(2){
                         font-size:rem(24);
                         color: #999999;
                         line-height:1rem;
                     }
                }
                &-log{            
                    float: left;
                    width: 1rem;
                    height: 1rem;
                    border-radius: 50%;
                    margin:0rem auto;
                    overflow: hidden;
                    img{
                        width: 100%;
                        height: 100%;
                    }
                } 
                &-icon{
                    margin-right: rem(10);
                    a{
                        margin-right:5px;
                        float:right;
                         display:block;
                        img{
                            width:rem(35);
                            height:rem(35);
                            vertical-align: middle;
                            padding-right:2px;
                           padding-bottom: 2px;
                        }
                        span{
                            padding:0 3px;
                            font-size: rem(24);
                            color: #999999;
                        }     
                    }
                    .red{
                        color: #ff5b52;
                    }
                }
            }
        }
    }
    .deleteDo {
        display: flex;
        width: 100%;
        position: fixed;
        left: 0;
        height: 1.3rem; // line-height: 1.3rem;
        color: #fff;
        bottom: 0;
        &-left {
            flex: 6;
            background: rgba(0, 0, 0, 0.4);
            border-right: 1px solid #fff;
            text-align: left;
            box-sizing: border-box;
            padding: 0 0.4rem; // input {
            span {
                float: right;
                line-height: 1.3rem;
            }
        }
        &-right {
            font-size: 0.3rem;
            flex: 3;
            background: rgba(0, 0, 0, 0.4);
            line-height: 1.3rem;
        }
    }
    .chk_1 {

        display: none; // vertical-align: -15%;
        width: 0.3rem;
        height: 0.3rem;
    }
    .chk_1+label {
        margin-top: 0.35rem;
        color:#fff !important;
        background-color: #FFF;
        border: 1px solid #C1CACA;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
        padding: 0.2rem;
        border-radius: 50%;
        display: inline-block;
        position: relative;
        margin-right: 30px; // display: none;
        // vertical-align: -15%;
        width: 0.1rem;
        height: 0.1rem;
    }
    .chk_1+label:active {

        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px 1px 3px rgba(0, 0, 0, 0.1);
    }
    .chk_1:checked+label {
        margin-top: 0.35rem;
        background-color: $primaryColor;
        border: 1px solid $primaryColor;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
        color: #243441;
    }
    .chk_1:checked+label:after {
        content: '\2714'; //勾选符号 
        position: absolute;
        top: 0px;
        left: 0px;
        color: #fff !important;
        width: 100%;
        text-align: center;
        font-size: 0.2rem; // padding: 1px 0 0 0;
        vertical-align: text-top;
    }
}
</style>




